<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Email_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
	
    /*
     * 邮件模版
     * */
    
    
    function insert_e_temp($tempname = '',$tempinfo = '',$temptitle = ''){
    	
    	$q = $this->db->insert('sdk_emailtemplate',array('tempname'=>$tempname,'tempinfo'=>$tempinfo,'temptitle'=>$temptitle));
    	
    	return $this->db->affected_rows()>0?true:false;
    }
    
    
    function update_e_temp($tempname = '',$tempinfo = '',$temptitle = ''){

    	
    	$q = $this->db->update('sdk_emailtemplate',array('tempinfo'=>$tempinfo,'temptitle'=>$temptitle),array('tempname'=>$tempname));
    	
    	if($this->db->affected_rows()>0){
    		
    		return true;
    		
    	}else{
    		
    		$q = $this->db->get_where('sdk_emailtemplate',array('tempname'=>$tempname));
    		
    		if($q->num_rows()>0) return false;
    		
    		$q = $this->insert_e_temp($tempname,$tempinfo,$temptitle);
    		
    		return $this->db->affected_rows()>0?true:false; 
    	}
    	
    }
    
    function temp_e_list($tempid = ''){
 
    	if($tempid){
    		
    		$q = $this->db->get_where('sdk_emailtemplate',array('id'=>$tempid));
    		return $q->row();
    	}else{
    		
    		$q = $this->db->get('sdk_emailtemplate');
    		return $q->result();
    	}
    	
    }
    
   /*
    * 邮件状态
    * */
    
    function insert_emailstatus($tempname='',$temp='',$conf=''){
    	
    	$sql = "INSERT INTO sdk_emailsend (creat_time,tempid,sendstatus,tempinfo,confinfo) values ('".time()."',(select id from sdk_emailtemplate where tempname='$tempname'),'0','$temp','$conf')";
    	
    	$this->db->query($sql);
    	
    	return $this->db->affected_rows()>0?true:false;
    }
    
    function get_es_list($status = '0'){
    	$sql = "SELECT * FROM sdk_emailsend WHERE sendstatus=0 and creat_time>".(time()-24*60*60);
    	
    	$q = $this->db->query($sql);
    	
    	return $q->result();
    }
    
    function update_emailstatus($id=''){
    	
    	$this->db->update('sdk_emailsend',array('sendstatus'=>1),array('id'=>$id));
    	
    }
    
    
    
    
}